| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace common\models\wechat;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\user\UserActionRecord;
- use common\models\user\UserInfo;
- use Yii;
- /**
- * This is the model class for table "{{%wechat_subscribe}}".
- *
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property string $to_user_name 开发者微信号
- * @property string $from_user_name 发送方帐号(一个OpenID)
- * @property int $create_time 消息创建时间
- * @property string $msg_type 消息类型,event
- * @property int $subscribe_status 订阅状态【1关注 2取消关注】
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class WechatSubscribe extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%wechat_subscribe}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'create_time', 'subscribe_status', 'status', 'created_at', 'updated_at'], 'integer'],
- [['to_user_name'], 'required'],
- [['to_user_name', 'from_user_name'], 'string', 'max' => 64],
- [['msg_type'], 'string', 'max' => 32],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'to_user_name' => '开发者微信号',
- 'from_user_name' => '发送方帐号(一个OpenID)',
- 'create_time' => '消息创建时间',
- 'msg_type' => '消息类型,event',
- 'subscribe_status' => '订阅状态【1关注 2取消关注】',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/12/9 0009 15:34
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $params
- * @return bool
- */
- public static function setData($mall_id, $params)
- {
- try {
- $is_first = false; // 是否第一次关注
- // 根据openid查找数据
- $model = self::find()->where(['mall_id' => $mall_id, 'from_user_name' => $params['from_user_name'], 'status' => StatusEnum::ENABLED])->one();
- if (empty($model)) {
- $is_first = true;
- $model = new WechatSubscribe();
- $model->mall_id = $mall_id;
- }
- if (!$model->load($params, '') || !$model->save()) throw new \Exception(WechatSubscribe::getStaticError());
- // 根据openid查找用户的user_id,修改用户行为记录
- $user_id = UserInfo::find()->select('user_id')->where(['openid' => $params['from_user_name'], 'status' => StatusEnum::ENABLED])->asArray()->scalar();
- if ($user_id) {
- $action['follow'] = $model->subscribe_status;
- $model->subscribe_status == 1 && $action['follow_at'] = time();
- $res = UserActionRecord::setData($mall_id, $user_id, $action);
- if ($res == false) throw new \Exception(UserActionRecord::getStaticError());
- }
- // TODO 订阅事件触发
- if ($is_first && $model->subscribe_status == 1) {
- // 触发首次关注礼包发放事件
- } elseif ($model->subscribe_status == 2) {
- // 触发取消关注事件
- }
- return true;
- } catch (\Exception $e) {
- $exception = FormatHelper::exception($e, "公众号订阅保存失败");
- self::setStaticError($exception);
- return false;
- }
- }
- }
|