ShareRedpackCode.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace addons\ShareRedpack\common\models;
  3. use common\enums\RedisKeyEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "qimall_addons_share_redpack_code".
  10. *
  11. * @property int $id ID
  12. * @property int $mall_id 商城ID
  13. * @property int $user_id 用户ID
  14. * @property int $source 来源,对应qimall_user.source
  15. * @property int $item_id 项目ID
  16. * @property string $code 分享码
  17. * @property string $content 二维码参数
  18. * @property string|null $applet_code
  19. * @property int $status 状态【1启用 0禁用 -1删除】
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 上次更新数据时间
  22. * @property int $store_id 门店id
  23. */
  24. class ShareRedpackCode extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return 'qimall_addons_share_redpack_code';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'user_id', 'source', 'item_id', 'status', 'store_id'], 'integer'],
  40. [['code'], 'string', 'max' => 16],
  41. [['content', 'applet_code'], 'string', 'max' => 255],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => 'Mall ID',
  52. 'user_id' => 'User ID',
  53. 'source' => 'Source',
  54. 'item_id' => 'Item ID',
  55. 'code' => 'Code',
  56. 'content' => 'Content',
  57. 'applet_code' => 'Applet Code',
  58. 'status' => 'Status',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. 'store_id' => 'Store ID',
  62. ];
  63. }
  64. /**
  65. * 获取并保存数据
  66. * @Author: lun
  67. * @DateTime: 2021/11/15 0015 15:09
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @param array $params
  70. * @return bool|array
  71. */
  72. public static function setData($mallId, $user_id, $source, array $params, $select = "",$code=''){
  73. try{
  74. $cond = [];
  75. !empty($params['id']) && $cond['id'] = $params['id'];
  76. $cond = ['mall_id' => $mallId, 'user_id' => $user_id, 'source' => $source, 'status'=>[StatusEnum::ENABLED]];
  77. !empty($params['item_id']) && $cond['item_id'] = $params['item_id'];
  78. $select = $select ?? "id,mall_id,user_id,source,code,content";
  79. $model = self::find()->select($select)->where($cond)->one();
  80. if(empty($model)) {
  81. $model = new self();
  82. $model->loadDefaultValues();
  83. $model->mall_id = $mallId;
  84. $model->user_id = $user_id;
  85. $model->source = $source;
  86. $model->item_id = $params['item_id'];
  87. $model->store_id = $params['store_id']??0;
  88. $model->code = empty($code)? self::getRandomCode($mallId) : $code;
  89. $model->content = Json::encode($params);
  90. if(!$model->save()){
  91. throw new Exception($model->getErrorMessage());
  92. }
  93. // 生成缓存
  94. $cache = Yii::$app->cache;
  95. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::POSTER_CODE , "M{$mallId}" . $model->code,true);
  96. $cache->set($cache_key, $model->content);
  97. }
  98. return $model->toArray();
  99. }catch(Exception $e){
  100. self::$static_error = $e->getMessage();
  101. return false;
  102. }
  103. }
  104. /**
  105. * 生成分享码
  106. * @Author: lun
  107. * @DateTime: 2021/11/15 0015 15:10
  108. * @Copyright: copyright (c) 2021 广东七件事集团
  109. * @param $mallId
  110. * @return false|string
  111. */
  112. public static function getRandomCode($mallId)
  113. {
  114. // 生成6位随机code
  115. $code = substr(md5(uniqid(md5(microtime(true)),true)), 0, 12);
  116. $data = self::getOne([['mall_id' => $mallId, 'code' => $code]], true, [], false, 'id');
  117. if ($data) {
  118. self::getRandomCode($mallId);
  119. } else {
  120. return $code;
  121. }
  122. }
  123. /**
  124. * 创建微信小程序二维码临时存放目录
  125. * @Author: lun
  126. * @DateTime: 2022/9/27 0027 16:54
  127. * @Copyright: copyright (c) 2021 广东七件事集团
  128. * @param $params
  129. */
  130. public static function mkdirQrcode($params)
  131. {
  132. mkdir($params['path'], 0755, true);
  133. exec("chown www:www {$params['path']}");
  134. }
  135. }