OaOut.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * OaOut.php UTF-8
  4. * 消息对外
  5. *
  6. * @date : 2018/9/13 22:09
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpMsg\controller;
  13. use huo\controller\common\Base;
  14. use huo\controller\member\MemCache;
  15. use huo\controller\wap\Option;
  16. use huo\model\member\MemoauthModel;
  17. use huo\model\user\UserModel;
  18. use huolib\constant\OptionConst;
  19. use huomp\controller\member\Member;
  20. use huomp\model\weixin\MpConfModel;
  21. use huomp\model\weixin\OaMpModel;
  22. use huoMpMsg\status\OfficialAccountStatus;
  23. use think\Log;
  24. class OaOut extends Base {
  25. const GOLD_NAME = '积分';
  26. const RP_NAME = '红包';
  27. const GOLD_UNIT = '积分';
  28. const RP_UNIT = '元';
  29. private $tpl_id = 'QnxQn_ylFFKbhRNG84hdtU9Lp9gbUXsU1WB1KeNXaJw';
  30. private $settle_tpl_id = 'Qp_aC8WXKSAw_ZlTRkx6vFespGXK5GRGnOWnpcdf6Ug';
  31. private $wx_app_id = 'wx34b5f6e35fae9903';
  32. public function __construct($wx_app_id = '') {
  33. if (!empty($wx_app_id)) {
  34. $this->wx_app_id = $wx_app_id;
  35. $_conf_id = (new MpConfModel())->getIdByMpId($wx_app_id);
  36. $this->setTplData($_conf_id);
  37. }
  38. }
  39. protected function setTplData($conf_id) {
  40. $_setting_name = OptionConst::WEIXIN_TEMPLATE_MSG.$conf_id;
  41. $_m = new Option();
  42. $_item = $_m->getOptionData($_setting_name, 1, true);
  43. if (!empty($_item['option_value'])) {
  44. $_option_value = json_decode($_item['option_value'], true);
  45. unset($_item);
  46. if (!empty($_option_value['tpl_id'])) {
  47. $this->tpl_id = $_option_value['tpl_id'];
  48. }
  49. if (!empty($_option_value['settle_tpl_id'])) {
  50. $this->settle_tpl_id = $_option_value['settle_tpl_id'];
  51. }
  52. }
  53. }
  54. /**
  55. * 发送模板金币变动信息
  56. *
  57. * @param int $mem_id 玩家ID
  58. * @param int $app_id 应用ID
  59. * @param int $change_gold 改变的金额
  60. * @param int $time 改变的时间戳
  61. *
  62. * @return array
  63. */
  64. public function sendGoldChangeMsg($mem_id, $app_id, $change_gold, $time = 0) {
  65. $_type_name = self::GOLD_NAME;
  66. $_oa_id = (new OaMpModel())->getOaByApp($app_id);
  67. $_gold = MemCache::ins()->getMyItg($mem_id);
  68. return $this->sendChangeMsg($mem_id, $_oa_id, $change_gold, $_gold, $time, $_type_name);
  69. }
  70. /**
  71. * 发送模板红包变动信息
  72. *
  73. * @param int $mem_id 玩家ID
  74. * @param int $app_id 应用ID
  75. * @param int $change_money 改变的金额
  76. * @param int $time 改变的时间戳
  77. *
  78. * @return array
  79. */
  80. public function sendMoneyChangeMsg($mem_id, $app_id, $change_money, $time = 0) {
  81. $_type_name = self::RP_NAME;
  82. $_oa_id = (new OaMpModel())->getOaByApp($app_id);
  83. $_balance = (new Member())->getBalance($mem_id);
  84. return $this->sendChangeMsg($mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name);
  85. }
  86. /**
  87. * 发送模板红包变动信息
  88. *
  89. * @param int $mem_id 玩家ID
  90. * @param int $wx_app_id 公众号应用ID
  91. * @param int $change_money 改变的金额
  92. * @param int $time 改变的时间戳
  93. *
  94. * @return array
  95. */
  96. public function sendWxMoneyChangeMsg($mem_id, $wx_app_id, $change_money, $time = 0) {
  97. $_type_name = self::RP_NAME;
  98. $_oa_id = $wx_app_id;
  99. $_balance = (new Member())->getBalance($mem_id);
  100. return $this->sendChangeMsg($mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name);
  101. }
  102. /**
  103. * 发送模板红包变动信息
  104. *
  105. * @param int $agent_id 玩家渠道ID
  106. * @param int $change_money 改变的金额
  107. * @param int $time 改变的时间戳
  108. *
  109. * @return array
  110. */
  111. public function sendAdminMoneyChangeMsg($agent_id, $change_money, $time = 0) {
  112. $_type_name = self::RP_NAME;
  113. $_oa_id = $this->wx_app_id;
  114. $_mem_id = (new UserModel())->getMemIdById($agent_id);
  115. $_balance = (new Member())->getBalance($_mem_id);
  116. return $this->sendChangeMsg($_mem_id, $_oa_id, $change_money, $_balance, $time, $_type_name);
  117. }
  118. /**
  119. * 发送模板帐户资金变动提醒信息
  120. *
  121. * @param int $mem_id 玩家ID
  122. * @param string $oa_id 公众号ID
  123. * @param double $change_money 改变金额
  124. * @param double $balance 余额
  125. * @param integer $time 修改时间
  126. * @param string $type_name 名称
  127. *
  128. * @return array
  129. */
  130. public function sendChangeMsg($mem_id, $oa_id, $change_money, $balance, $time, $type_name = '') {
  131. $_wx_app_id = $oa_id;
  132. $_mc_model = new MpConfModel();
  133. $_conf_id = $_mc_model->getIdByMpId($_wx_app_id);
  134. $this->setTplData($_conf_id);
  135. $_open_id = (new MemoauthModel())->getOpenIdByMpMemId($_conf_id, $mem_id);
  136. if (empty($_open_id)) {
  137. $_code = OfficialAccountStatus::OPENID_IS_NULL;
  138. Log::write(
  139. "func=".__FUNCTION__."&class=".__CLASS__."&conf_id=".$_conf_id."&code=".$_code.'&msg='
  140. .OfficialAccountStatus::getMsg($_code)
  141. ."&mem_id=".$mem_id.'&oa_id='.$oa_id.'&change_money='.$change_money.'&balance='.$balance.'&type_name='
  142. .$type_name,
  143. LOG::WECHAT, true
  144. );
  145. return $this->huoError($_code, OfficialAccountStatus::getMsg($_code));
  146. }
  147. $_template_id = $this->getTplData($_wx_app_id, '');
  148. $_wx_app_secret = $_mc_model->getSecretByMpId($_wx_app_id);
  149. $_time = !empty($time) ? $time : time();
  150. if (self::GOLD_NAME == $type_name) {
  151. $change_money = $change_money.self::GOLD_UNIT;
  152. $balance = $balance.self::GOLD_UNIT;
  153. } elseif (self::RP_NAME == $type_name) {
  154. $change_money = '¥'.$change_money.self::RP_UNIT;
  155. $balance = '¥'.$balance.self::RP_UNIT;
  156. }
  157. $_data = [
  158. 'first' => [
  159. 'value' => '您好,您有新的余额变动',
  160. 'color' => '#173177'
  161. ],
  162. 'date' => [
  163. 'value' => date('Y-m-d H:i:s', $_time),
  164. 'color' => '#173177'
  165. ],
  166. 'adCharge' => [
  167. 'value' => $change_money,
  168. 'color' => '#173177'
  169. ],
  170. 'type' => [
  171. 'value' => $type_name,
  172. 'color' => '#173177'
  173. ],
  174. 'cashBalance' => [
  175. 'value' => $balance,
  176. 'color' => '#173177'
  177. ],
  178. 'remark' => [
  179. 'value' => '点击"详情"立即查阅您的帐户财务记录。',
  180. 'color' => '#173177'
  181. ],
  182. ];
  183. $_url = $this->getUrl($_wx_app_id);
  184. $_rs = (new OfficialAccountApi())->sendTemplateMsg(
  185. $_wx_app_id, $_wx_app_secret, $_open_id, $_template_id, $_url, [], $_data
  186. );
  187. return $this->huoReturn($_rs);
  188. }
  189. /**
  190. * 发送模板红包变动信息
  191. *
  192. * @param int $agent_id 玩家渠道ID
  193. * @param int $change_money 改变的金额
  194. * @param int $time 改变的时间戳
  195. *
  196. * @return array
  197. */
  198. public function sendAdminSettleMsg($agent_id, $change_money, $time = 0) {
  199. $_type_name = self::RP_NAME;
  200. $_oa_id = $this->wx_app_id;
  201. $_mem_id = (new UserModel())->getMemIdById($agent_id);
  202. return $this->sendSettleMsg($_mem_id, $_oa_id, $change_money, $time, $_type_name);
  203. }
  204. /**
  205. * 发送模板提现到账提醒信息
  206. *
  207. * @param int $mem_id 玩家ID
  208. * @param string $oa_id 公众号ID
  209. * @param double $change_money 改变金额
  210. * @param integer $time 修改时间
  211. * @param string $type_name 名称
  212. *
  213. * @return array
  214. */
  215. public function sendSettleMsg($mem_id, $oa_id, $change_money, $time, $type_name = self::RP_NAME) {
  216. $_wx_app_id = $oa_id;
  217. $_mc_model = new MpConfModel();
  218. $_conf_id = $_mc_model->getIdByMpId($_wx_app_id);
  219. $_oauth_data = (new MemoauthModel())->getDataByMpMemId($_conf_id, $mem_id);
  220. $_open_id = $_oauth_data['openid'];
  221. $_nickname = $_oauth_data['nickname'];
  222. if (empty($_open_id)) {
  223. $_code = OfficialAccountStatus::OPENID_IS_NULL;
  224. Log::write(
  225. "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.OfficialAccountStatus::getMsg($_code)
  226. ."&mem_id=".$mem_id.'&oa_id='.$oa_id.'&change_money='.$change_money.'&type_name'
  227. .$type_name,
  228. LOG::ERROR
  229. );
  230. return $this->huoError($_code, OfficialAccountStatus::getMsg($_code));
  231. }
  232. $_template_id = $this->getSettleTplData($_wx_app_id, '');
  233. $_wx_app_secret = $_mc_model->getSecretByMpId($_wx_app_id);
  234. $_time = !empty($time) ? $time : time();
  235. if (self::GOLD_NAME == $type_name) {
  236. $change_money = $change_money.self::GOLD_UNIT;
  237. } elseif (self::RP_NAME == $type_name) {
  238. $change_money = $change_money.self::RP_UNIT;
  239. }
  240. $_data = [
  241. 'first' => [
  242. 'value' => '尊敬的'.$_nickname.',您提现已处理成功。',
  243. 'color' => '#173177'
  244. ],
  245. 'keyword1' => [
  246. 'value' => date('Y-m-d H:i:s', $_time),
  247. 'color' => '#173177'
  248. ],
  249. 'keyword2' => [
  250. 'value' => '¥'.$change_money,
  251. 'color' => '#173177'
  252. ],
  253. 'remark' => [
  254. 'value' => '请留意提现卡号余额变动。',
  255. 'color' => '#173177'
  256. ],
  257. ];
  258. $_url = $this->getUrl($_wx_app_id);
  259. $_rs = (new OfficialAccountApi())->sendTemplateMsg(
  260. $_wx_app_id, $_wx_app_secret, $_open_id, $_template_id, $_url, [], $_data
  261. );
  262. return $this->huoReturn($_rs);
  263. }
  264. /**
  265. * @param string $mp_id
  266. * @param string $type
  267. *
  268. * @return string
  269. */
  270. public function getTplData($mp_id, $type = '') {
  271. return $this->tpl_id;
  272. }
  273. /**
  274. * @param string $mp_id
  275. * @param string $type
  276. *
  277. * @return string
  278. */
  279. public function getSettleTplData($mp_id, $type = '') {
  280. return $this->settle_tpl_id;
  281. }
  282. /**
  283. * @param string $mp_id
  284. *
  285. * @return string
  286. */
  287. public function getUrl($mp_id) {
  288. return MPAPISITE.'/index/income/list/'.$mp_id;
  289. }
  290. public function getJsapiTicket($oa_id) {
  291. $_wx_app_id = $oa_id;
  292. $_mc_model = new MpConfModel();
  293. $_mp_data = $_mc_model->getDataByMpId($_wx_app_id);
  294. $_access_token = Common::getAccessToken($_mp_data['mp_id'], $_mp_data['app_secret']);
  295. if (empty($_access_token)) {
  296. $_code = OfficialAccountStatus::ACCESS_TOKEN_IS_NULL;
  297. Log::write(
  298. "func=".__FUNCTION__."&class=".__CLASS__."&oa_id=".$oa_id."&code=".$_code.'&msg='
  299. .OfficialAccountStatus::getMsg($_code).'&mp_data='.json_encode($_mp_data), LOG::ERROR
  300. );
  301. return $this->huoError($_code, OfficialAccountStatus::getMsg($_code));
  302. }
  303. $_ticket = Common::getJsapiTicket($_access_token);
  304. if (empty($_ticket)) {
  305. $_code = OfficialAccountStatus::TICKET_IS_NULL;
  306. Log::write(
  307. "func=".__FUNCTION__."&class=".__CLASS__."&oa_id=".$oa_id."&code=".$_code.'&msg='
  308. .OfficialAccountStatus::getMsg($_code).'&access_token='.$_access_token, LOG::ERROR
  309. );
  310. return $this->huoError($_code, OfficialAccountStatus::getMsg($_code));
  311. }
  312. $_rdata = [
  313. 'code' => OfficialAccountStatus::NO_ERROR,
  314. 'msg' => OfficialAccountStatus::getMsg(OfficialAccountStatus::NO_ERROR),
  315. 'data' => $_ticket
  316. ];
  317. return $this->huoReturn($_rdata);
  318. }
  319. }