IMFriend.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace App\Console\Commands\Upgrade;
  3. use App\Managers\IMManager;
  4. use App\Models\Friends\LogModel;
  5. use App\Models\IM\UserLatestMessage;
  6. use App\Models\User\LikeInviteQuestionModel;
  7. use App\Models\User\UserModel;
  8. use App\Utils\ImUtil;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Arr;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. class IMFriend extends Command
  14. {
  15. protected $signature = "upgrade:im-friend {func?}";
  16. protected $description = "版本升级:APP-v1.1.3 IM";
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. }
  21. public function handle()
  22. {
  23. $func = $this->argument('func');
  24. switch ($func) {
  25. case 'registerim':
  26. $this->userRegisterIm();
  27. return;
  28. default:
  29. break;
  30. }
  31. return;
  32. // $this->updateChargeUserTable();
  33. $this->userRegisterIm();
  34. $this->updateFriendTable();
  35. $this->updateFriendApplyTable();
  36. $this->oldFriendMappingNewFriend();
  37. $this->createUserLatestMessageTable();
  38. $this->createUserMessageTable();
  39. $this->oldMessage2NewMessage();
  40. }
  41. public function updateFriendTable()
  42. {
  43. $sql1 = "alter table kdgx_fpdx_friends add list_unread_msg_cnt int(10) default 0 not null comment '列表未读数量' after last_msg;";
  44. DB::statement($sql1);
  45. $sql2 = "alter table kdgx_fpdx_friends add friend_level tinyint(4) default 0 not null comment '亲密度等级0陌生人1普通联系人2好友联系人' after friend_uid;";
  46. DB::statement($sql2);
  47. $sql3 = "alter table kdgx_fpdx_friends add is_blacklist tinyint(4) default 0 not null comment '是否拉黑0否1是' after friend_level;";
  48. DB::statement($sql3);
  49. $sql4 = "alter table kdgx_fpdx_friends add note char(16) comment '备注';";
  50. DB::statement($sql4);
  51. }
  52. public function updateFriendApplyTable()
  53. {
  54. $sql4 = "alter table kdgx_fpdx_friend_apply add handle tinyint(4) default 0 not null comment '是否处理-1忽略;0未处理;1同意' after apply_uid;";
  55. DB::statement($sql4);
  56. $sql5 = "alter table kdgx_fpdx_friend_apply add send_unread tinyint(4) default 0 not null comment '发送者是否已读0未读1已读';";
  57. DB::statement($sql5);
  58. $sql6 = "alter table kdgx_fpdx_friend_apply add receive_unread tinyint(4) default 1 not null comment '接收者者是否已读0未读1已读';";
  59. DB::statement($sql6);
  60. }
  61. public function updateChargeUserTable()
  62. {
  63. $sql5 = "alter table kdgx_partner_charge_user add im_account char(128) null comment 'ImAccount', add im_sig char(128) null comment 'ImSig'";
  64. DB::statement($sql5);
  65. $sql6 = "ALTER TABLE `kdgx_partner_charge_user` ADD unique key `im_account`(`im_account`);";
  66. DB::statement($sql6);
  67. }
  68. public function createPairApplyTable()
  69. {
  70. $sql6 = "create table pair_contact_applies (
  71. `id` int(10) not null auto_increment comment '主键',
  72. `created_at` int(10) not null comment '发出时间',
  73. `send_uid` int(10) not null comment '用户',
  74. `receive_uid` int(10) not null comment '邀请用户',
  75. `activity_id` int(10) not null comment '活动id',
  76. `handle` tinyint(4) default 0 not null comment '是否处理-1忽略;0未处理;1同意',
  77. primary key (`id`),
  78. index idx_uid(`send_uid`),
  79. index idx_apply_uid(`receive_uid`))comment '分配对象CP申请表';";
  80. DB::statement($sql6);
  81. }
  82. public function createPairApplyListShareLog()
  83. {
  84. $sql = "create table pair_apply_list_share_help_log(
  85. `id` int(10) not null auto_increment comment '主键',
  86. `created_at` int(10) not null,
  87. `share_uid` int(10) not null comment '分享用户',
  88. `help_uid` int(10) not null comment '助力用户',
  89. `lock_uid` int(10) not null comment '解锁用户',
  90. `stage_id` int(10) not null comment '期数',
  91. primary key (`id`)
  92. )comment '可发起CP申请的用户列表分享助力记录'";
  93. DB::statement($sql);
  94. }
  95. public function oldFriendMappingNewFriend()
  96. {
  97. DB::transaction(function () {
  98. $table = "kdgx_fpdx_friends";
  99. $tableTmp = "{$table}_tmp";
  100. DB::update('create table ' . $tableTmp . ' like ' . $table);
  101. $sql = "insert {$tableTmp}(id, created_at, uid, friend_uid, friend_level, is_blacklist, last_at, star_at, unread_cnt, last_msg, list_unread_msg_cnt, each_like, goodnight, is_hide, friend_type, friend_type_data)
  102. select id,created_at, uid, friend_uid, IF(1 = is_friend, 2, IF(1 = each_like, 1, 0)), is_blacklist, last_at, star_at, unread_cnt, last_msg, list_unread_msg_cnt, each_like, goodnight, is_hide, friend_type, friend_type_data from {$table};";
  103. DB::update($sql);
  104. $sql = "delete from {$tableTmp} using {$tableTmp},
  105. (select uid,friend_uid from {$tableTmp} where is_hide=1) as h
  106. where ({$tableTmp}.uid=h.uid and {$tableTmp}.friend_uid=h.friend_uid)
  107. or ({$tableTmp}.uid=h.friend_uid and {$tableTmp}.friend_uid=h.uid)";
  108. DB::update($sql);
  109. $sql = "alter table {$tableTmp}
  110. drop column friend_type,
  111. drop column friend_type_data,
  112. drop column is_hide,
  113. drop column is_friend,
  114. drop column unfriend_at,
  115. drop column gteam";
  116. DB::update($sql);
  117. $sql = "rename table {$table} to {$table}_copy;rename table {$tableTmp} to {$table};";
  118. DB::update($sql);
  119. });
  120. }
  121. // 用户批量注册IM
  122. public function userRegisterIm()
  123. {
  124. $imUtil = new ImUtil();
  125. UserModel::whereNull('im_account')->each(function (/** @var UserModel $item */$item) use ($imUtil) {
  126. $item->im_account = md5(config('app.env') . $item->uid);
  127. $imSig = $imUtil->registerUser($item->im_account, $item->nickname, $item->headimgurl, $item->sex, 0);
  128. $item->im_sig = $imSig;
  129. $item->save();
  130. });
  131. }
  132. public function createUserLatestMessageTable()
  133. {
  134. $sql = "create table user_latest_messages(
  135. `uid` bigint(20) not null,
  136. `from_uid` bigint(20) not null,
  137. `to_uid` bigint(10) not null,
  138. `seq` bigint(20) null,
  139. `msg_random` int(10) not null,
  140. `msg_time_stamp` int(10) not null,
  141. `read` boolean not null,
  142. `content` longtext null,
  143. key `idx_uid_from_to` (`uid`, `from_uid`, `to_uid`)
  144. )comment '用户消息表'";
  145. DB::connection('mysql_fenpeiduixiang')->update($sql);
  146. }
  147. public function createUserMessageTable()
  148. {
  149. $sql = "create table user_messages(
  150. `uid` bigint(20) not null,
  151. `from_uid` bigint(20) not null,
  152. `to_uid` bigint(10) not null,
  153. `seq` bigint(20) null,
  154. `msg_random` int(10) not null,
  155. `msg_time_stamp` int(10) not null,
  156. `read` boolean not null,
  157. `content` longtext null,
  158. key `idx_uid_from_to` (`uid`, `from_uid`, `to_uid`)
  159. )comment '用户消息日志表'";
  160. DB::connection('mysql_fenpeiduixiang')->update($sql);
  161. }
  162. public function oldMessage2NewMessage()
  163. {
  164. $lastId = Redis::hget("upgrade", "im-oldmsg2newmsg") ?? 0;
  165. $imManager = new IMManager();
  166. LogModel::where('do', 3)->where('id', '>', $lastId)->orderBy('id')->each(function ($item) use ($imManager) {
  167. Redis::hset("upgrade", "im-oldmsg2newmsg", $item->id);
  168. try {
  169. $fromUser = UserModel::whereUid($item->uid)->first('im_account');
  170. $toUser = UserModel::whereUid($item->uid)->first('im_account');
  171. $fromAccount = $fromUser->im_account;
  172. $toAccount = $toUser->im_account;
  173. $msgTime = $item->created_at;
  174. switch ($item->attach['type']) {
  175. case 2:
  176. // 心动问答
  177. $question = LikeInviteQuestionModel::withTrashed()->find($item->attach['question']);
  178. $question->question = json_decode($question->question, true);
  179. $msgData = [
  180. 'MsgType' => 'LikeQA',
  181. 'Content' => [
  182. 'question' => [
  183. 'title' => Arr::get($question->question, 'question'),
  184. 'id' => Arr::get($question->question, 'id')
  185. ],
  186. 'answer' => [
  187. 'type' => Arr::get($item->attach, 'type'),
  188. 'value' => Arr::get($item->attach, 'value')
  189. ]
  190. ]
  191. ];
  192. break;
  193. case 3:
  194. // 唱首歌
  195. $question = LikeInviteQuestionModel::withTrashed()->find($item->attach['question']);
  196. $question->question = json_decode($question->question, true);
  197. $msgData = [
  198. 'MsgType' => 'LikeSing',
  199. 'Content' => [
  200. 'song' => [
  201. 'name' => Arr::get($question->question, 'question.name'),
  202. 'singer' => Arr::get($question->question, 'question.author'),
  203. ],
  204. 'answer' => [
  205. 'type' => Arr::get($item->attach, 'type'),
  206. 'value' => Arr::get($item->attach, 'value')
  207. ]
  208. ]
  209. ];
  210. break;
  211. case 4:
  212. // 走心一画
  213. $msgData = [
  214. 'MsgType' => 'LikeDraw',
  215. 'Content' => [
  216. 'paint' => [
  217. 'id' => Arr::get($item->attach, 'value.paint_id'),
  218. 'name' => Arr::get($item->attach, 'value.name'),
  219. 'url' => Arr::get($item->attach, 'value.pic_url'),
  220. ]
  221. ]
  222. ];
  223. break;
  224. default:
  225. return;
  226. }
  227. $data = array(
  228. 'MsgBody' => [
  229. [
  230. 'MsgType' => 'TIMCustomElem',
  231. 'MsgContent' => [
  232. 'Data' => $msgData,
  233. 'Desc' => null,
  234. 'Ext' => null,
  235. 'Sound' => null
  236. ]
  237. ]
  238. ],
  239. "CallbackCommand" => "C2C.CallbackAfterSendMsg",
  240. 'From_Account' => $fromAccount,
  241. 'To_Account' => $toAccount,
  242. 'MsgRandom' => rand(1, 500000000),
  243. 'MsgSeq' => rand(0, 7000),
  244. 'MsgTime' => $msgTime,
  245. 'ClientIP' => '183.129.21.216',
  246. 'OptPlatform' => 'Android',
  247. 'RequestId' => '4144894721-144115214975491767-1572409801-500726985',
  248. "SdkAppid" => "1400263610",
  249. "contenttype" => "json"
  250. );
  251. $fromAccount = $data['From_Account'];
  252. $toAccount = $data['To_Account'];
  253. $msgTime = $data['MsgTime'];
  254. $msgRandom = $data['MsgRandom'];
  255. $msgSeq = $data['MsgSeq'];
  256. $imManager->createUserLatestMessageByAccount(
  257. $fromAccount,
  258. $fromAccount,
  259. $toAccount,
  260. $msgTime,
  261. $msgRandom,
  262. $msgSeq,
  263. $data,
  264. true
  265. );
  266. $imManager->createUserLatestMessageByAccount(
  267. $toAccount,
  268. $fromAccount,
  269. $toAccount,
  270. $msgTime,
  271. $msgRandom,
  272. $msgSeq,
  273. $data,
  274. true
  275. );
  276. } catch (\Exception $exception) {
  277. return;
  278. } catch (\Throwable $exception) {
  279. return;
  280. } finally {
  281. return;
  282. }
  283. });
  284. }
  285. }