| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace addons\ShareRedpack\common\models;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "qimall_addons_share_redpack_code".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $source 来源,对应qimall_user.source
- * @property int $item_id 项目ID
- * @property string $code 分享码
- * @property string $content 二维码参数
- * @property string|null $applet_code
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- * @property int $store_id 门店id
- */
- class ShareRedpackCode extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_share_redpack_code';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'source', 'item_id', 'status', 'store_id'], 'integer'],
- [['code'], 'string', 'max' => 16],
- [['content', 'applet_code'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'source' => 'Source',
- 'item_id' => 'Item ID',
- 'code' => 'Code',
- 'content' => 'Content',
- 'applet_code' => 'Applet Code',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'store_id' => 'Store ID',
- ];
- }
- /**
- * 获取并保存数据
- * @Author: lun
- * @DateTime: 2021/11/15 0015 15:09
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return bool|array
- */
- public static function setData($mallId, $user_id, $source, array $params, $select = "",$code=''){
- try{
- $cond = [];
- !empty($params['id']) && $cond['id'] = $params['id'];
- $cond = ['mall_id' => $mallId, 'user_id' => $user_id, 'source' => $source, 'status'=>[StatusEnum::ENABLED]];
- !empty($params['item_id']) && $cond['item_id'] = $params['item_id'];
- $select = $select ?? "id,mall_id,user_id,source,code,content";
- $model = self::find()->select($select)->where($cond)->one();
- if(empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mallId;
- $model->user_id = $user_id;
- $model->source = $source;
- $model->item_id = $params['item_id'];
- $model->store_id = $params['store_id']??0;
- $model->code = empty($code)? self::getRandomCode($mallId) : $code;
- $model->content = Json::encode($params);
- if(!$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- // 生成缓存
- $cache = Yii::$app->cache;
- $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::POSTER_CODE , "M{$mallId}" . $model->code,true);
- $cache->set($cache_key, $model->content);
- }
- return $model->toArray();
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 生成分享码
- * @Author: lun
- * @DateTime: 2021/11/15 0015 15:10
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mallId
- * @return false|string
- */
- public static function getRandomCode($mallId)
- {
- // 生成6位随机code
- $code = substr(md5(uniqid(md5(microtime(true)),true)), 0, 12);
- $data = self::getOne([['mall_id' => $mallId, 'code' => $code]], true, [], false, 'id');
- if ($data) {
- self::getRandomCode($mallId);
- } else {
- return $code;
- }
- }
- /**
- * 创建微信小程序二维码临时存放目录
- * @Author: lun
- * @DateTime: 2022/9/27 0027 16:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- */
- public static function mkdirQrcode($params)
- {
- mkdir($params['path'], 0755, true);
- exec("chown www:www {$params['path']}");
- }
- }
|