WechatSubscribe.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\models\wechat;
  3. use common\enums\StatusEnum;
  4. use common\helpers\FormatHelper;
  5. use common\models\user\UserActionRecord;
  6. use common\models\user\UserInfo;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%wechat_subscribe}}".
  10. *
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城ID
  14. * @property string $to_user_name 开发者微信号
  15. * @property string $from_user_name 发送方帐号(一个OpenID)
  16. * @property int $create_time 消息创建时间
  17. * @property string $msg_type 消息类型,event
  18. * @property int $subscribe_status 订阅状态【1关注 2取消关注】
  19. * @property int $status 状态【1启用 0禁用 -1删除】
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 更新时间
  22. */
  23. class WechatSubscribe extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%wechat_subscribe}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'create_time', 'subscribe_status', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['to_user_name'], 'required'],
  40. [['to_user_name', 'from_user_name'], 'string', 'max' => 64],
  41. [['msg_type'], 'string', 'max' => 32],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'to_user_name' => '开发者微信号',
  53. 'from_user_name' => '发送方帐号(一个OpenID)',
  54. 'create_time' => '消息创建时间',
  55. 'msg_type' => '消息类型,event',
  56. 'subscribe_status' => '订阅状态【1关注 2取消关注】',
  57. 'status' => '状态【1启用 0禁用 -1删除】',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '更新时间',
  60. ];
  61. }
  62. /**
  63. * 保存数据
  64. * @Author: lun
  65. * @DateTime: 2021/12/9 0009 15:34
  66. * @Copyright: copyright (c) 2021 广东七件事集团
  67. * @param $mall_id
  68. * @param $params
  69. * @return bool
  70. */
  71. public static function setData($mall_id, $params)
  72. {
  73. try {
  74. $is_first = false; // 是否第一次关注
  75. // 根据openid查找数据
  76. $model = self::find()->where(['mall_id' => $mall_id, 'from_user_name' => $params['from_user_name'], 'status' => StatusEnum::ENABLED])->one();
  77. if (empty($model)) {
  78. $is_first = true;
  79. $model = new WechatSubscribe();
  80. $model->mall_id = $mall_id;
  81. }
  82. if (!$model->load($params, '') || !$model->save()) throw new \Exception(WechatSubscribe::getStaticError());
  83. // 根据openid查找用户的user_id,修改用户行为记录
  84. $user_id = UserInfo::find()->select('user_id')->where(['openid' => $params['from_user_name'], 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  85. if ($user_id) {
  86. $action['follow'] = $model->subscribe_status;
  87. $model->subscribe_status == 1 && $action['follow_at'] = time();
  88. $res = UserActionRecord::setData($mall_id, $user_id, $action);
  89. if ($res == false) throw new \Exception(UserActionRecord::getStaticError());
  90. }
  91. // TODO 订阅事件触发
  92. if ($is_first && $model->subscribe_status == 1) {
  93. // 触发首次关注礼包发放事件
  94. } elseif ($model->subscribe_status == 2) {
  95. // 触发取消关注事件
  96. }
  97. return true;
  98. } catch (\Exception $e) {
  99. $exception = FormatHelper::exception($e, "公众号订阅保存失败");
  100. self::setStaticError($exception);
  101. return false;
  102. }
  103. }
  104. }