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']}"); } }