ShareController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * ShareController.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/26 15:48
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace h5wap\wap\controller;
  13. use h5wap\common\controller\V2BaseController;
  14. use huo\controller\integral\MemIa;
  15. use huo\controller\posts\ShareAct;
  16. use huo\model\integral\MemItgLogModel;
  17. use huolib\constant\IaConst;
  18. use huolib\constant\ShareConst;
  19. use huolib\status\CommonStatus;
  20. use huolib\status\MemberStatus;
  21. use huolib\tool\StrUtils;
  22. use huolib\tool\Time;
  23. use huoMpMsg\status\OfficialAccountStatus;
  24. class ShareController extends V2BaseController {
  25. public function _initialize() {
  26. parent::_initialize();
  27. }
  28. /**
  29. * 分享成功后回调接口
  30. * http://doc.1tsdk.com/138?page_id=3359
  31. * 【域名】/wap/share/add
  32. */
  33. public function add() {
  34. $_mem_id = $this->mem_id;
  35. if (empty($_mem_id)) {
  36. $_code = MemberStatus::NO_ERROR;
  37. $this->success(lang(MemberStatus::getMsg($_code)), [], $_code);
  38. }
  39. $_game_rq = $this->setGameData();
  40. $_channel_rq = $this->setChannelData();
  41. $_device_rq = $this->setDeviceData();
  42. $_mem_rq = $this->setMemData();
  43. $_share_type = $this->request->param('share_type/s', '');
  44. if (empty($_share_type)) {
  45. $_code = CommonStatus::INVALID_PARAMS;
  46. $this->error(lang(CommonStatus::getMsg($_code)), [], $_code);
  47. }
  48. if (ShareConst::SHARE_TYPE_POSTS == $_share_type) {
  49. $_param = $this->request->param();
  50. $_act_id = $this->request->param('share_id/d', 0);
  51. if (empty($_act_id)) {
  52. $_code = CommonStatus::INVALID_PARAMS;
  53. $this->error(lang(CommonStatus::getMsg($_code)), [], $_code);
  54. }
  55. //分享成功回调处理
  56. $_rs = (new ShareAct())->callback($_game_rq, $_channel_rq, $_device_rq, $_mem_rq, $_param);
  57. return $this->returnData($_rs);
  58. } else {
  59. // TODO: liuhongliang 2018/5/28 其他分享类型
  60. $_code = MemberStatus::NO_ERROR;
  61. $this->success(lang(MemberStatus::getMsg($_code)), [], $_code);
  62. }
  63. }
  64. /**
  65. * 获取微信分享配置
  66. */
  67. public function config() {
  68. $this->checkLogin();
  69. $_share_url = $this->request->param('share_url/s', '');
  70. if (empty($_share_url)) {
  71. $_code = CommonStatus::INVALID_PARAMS;
  72. $this->error(CommonStatus::getMsg($_code), [], $_code);
  73. }
  74. $_wx_app_id = $this->getDefaultWxAppId();
  75. if (empty($_wx_app_id)) {
  76. $_code = CommonStatus::INNER_ERROR;
  77. $this->error(CommonStatus::getMsg($_code), [], $_code);
  78. }
  79. $_rs = $this->getJsapiTicket();
  80. $_share_url = urldecode($_share_url);
  81. $_rdata = [
  82. 'noncestr' => StrUtils::getRandChars(16),
  83. 'jsapi_ticket' => $_rs['data'],
  84. 'timestamp' => time(),
  85. 'url' => $_share_url
  86. ];
  87. ksort($_rdata);
  88. $_sig_str = 'jsapi_ticket='.$_rdata['jsapi_ticket'].'&noncestr='.$_rdata['noncestr'].'&timestamp='
  89. .$_rdata['timestamp'].'&url='.$_rdata['url'];
  90. $_rdata['signature'] = sha1($_sig_str);
  91. $_rdata['wx_app_id'] = $_wx_app_id;
  92. /* 读取配置 */
  93. $_share_config = [
  94. 'share_title' => '分享好游戏',
  95. 'share_content' => '多款精品游戏一起玩!',
  96. 'share_icon' => '/upload/icon6.png',
  97. ];
  98. $_rdata['title'] = get_val($_share_config, 'share_title', '分享标题~');
  99. $_rdata['content'] = get_val($_share_config, 'share_content', '分享内容~');
  100. $_icon = get_val($_share_config, 'share_icon', '');
  101. $_rdata['icon'] = cmf_get_image_url($_icon);
  102. $rdata['code'] = CommonStatus::NO_ERROR;
  103. $rdata['msg'] = CommonStatus::getMsg(CommonStatus::NO_ERROR);
  104. $rdata['data'] = $_rdata;
  105. $this->returnData($rdata);
  106. }
  107. /**
  108. * 分享通知接口
  109. *
  110. * @throws \think\Exception
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function shareNotify() {
  116. $_today_is_share = $this->getTodayIsShare($this->mem_id);
  117. if (!$_today_is_share) {
  118. (new MemIa($this->mem_id))->doItgAct(IaConst::IA_WX_SHARE);
  119. }
  120. }
  121. /**
  122. * 获取今日是否分享
  123. *
  124. * @param $mem_id
  125. *
  126. * @return bool
  127. * @throws \think\Exception
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. private function getTodayIsShare($mem_id) {
  133. $_map['mem_id'] = $mem_id;
  134. $_map['ia_id'] = IaConst::IA_WX_SHARE;
  135. list($_start_time, $_end_time) = Time::today();
  136. $_map['create_time'] = ['BETWEEN', [$_start_time, $_end_time]];
  137. $_detail = (new MemItgLogModel())->getDetail($_map);
  138. if (!empty($_detail)) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. private function getDefaultWxAppId() {
  144. if (file_exists(GLOBAL_CONF_PATH."extra/oauth/weixin.php")) {
  145. $_config = include GLOBAL_CONF_PATH."extra/oauth/weixin.php";
  146. }
  147. $_wx_app_id = !empty($_config['APP_KEY']) ? $_config['APP_KEY'] : '';
  148. return $_wx_app_id;
  149. }
  150. public function getJsapiTicket() {
  151. $_config = [];
  152. if (file_exists(GLOBAL_CONF_PATH."extra/oauth/weixin.php")) {
  153. $_config = include GLOBAL_CONF_PATH."extra/oauth/weixin.php";
  154. }
  155. $_mp_data = $_config;
  156. $_mp_data['mp_id'] = $_config['APP_KEY'];
  157. $_mp_data['app_secret'] = $_config['APP_SECRET'];
  158. $_access_token = $this->getAccessToken($_mp_data['mp_id'], $_mp_data['app_secret']);
  159. if (empty($_access_token)) {
  160. return false;
  161. }
  162. $_ticket = $this->getJsapiTicket1($_access_token);
  163. if (empty($_ticket)) {
  164. return false;
  165. }
  166. $_rdata = [
  167. 'code' => OfficialAccountStatus::NO_ERROR,
  168. 'msg' => OfficialAccountStatus::getMsg(OfficialAccountStatus::NO_ERROR),
  169. 'data' => $_ticket
  170. ];
  171. return $_rdata;
  172. }
  173. /**
  174. * 获取 access_token
  175. * https://developers.weixin.qq.com/miniprogram/dev/api/token.html
  176. *
  177. * @param string $appid 第三方用户唯一凭证
  178. * @param string $app_secret 第三方用户唯一凭证密钥,即appsecret
  179. *
  180. * @return string|bool
  181. */
  182. public static function getAccessToken($appid, $app_secret) {
  183. $_url = 'https://api.weixin.qq.com/cgi-bin/token';
  184. $_param['grant_type'] = 'client_credential';
  185. $_param['appid'] = $appid;
  186. $_param['secret'] = $app_secret;
  187. $_rdata = self::curl($_url, http_build_query($_param));
  188. $_rdata = json_decode($_rdata, true);
  189. if (!isset($_rdata['access_token'])) {
  190. return false;
  191. }
  192. $_data['access_token'] = $_rdata['access_token'];
  193. $_data['expires_in'] = $_rdata['expires_in'];
  194. $_data['expires_time'] = time() + $_rdata['expires_in'];
  195. return $_data['access_token'];
  196. }
  197. /**
  198. * CURL请求
  199. *
  200. * @param string $url 请求的URL
  201. * @param string $params 请求的参数
  202. * @param string $is_get 是否是get请求
  203. * @param array $header 头
  204. *
  205. * @return int|mixed
  206. */
  207. public static function curl($url, $params, $is_get = "GET", $header = []) {
  208. $_header = $header;
  209. $ch = curl_init();
  210. if ("GET" == $is_get) {
  211. if (!empty($params)) {
  212. $url = $url.'?'.$params;
  213. }
  214. curl_setopt($ch, CURLOPT_HEADER, 0); // 过滤HTTP头
  215. } else {
  216. curl_setopt($ch, CURLOPT_POST, 1);
  217. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  218. }
  219. curl_setopt($ch, CURLOPT_URL, $url);
  220. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  221. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  222. /* https 请求 */
  223. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  224. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  225. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  226. }
  227. if (!empty($_header)) {
  228. array_push($_header, 'Content-Length: '.strlen($params));
  229. curl_setopt($ch, CURLOPT_HTTPHEADER, $_header);
  230. }
  231. $_rs = curl_exec($ch);
  232. if (false === $_rs) {
  233. $_rs = curl_errno($ch);
  234. }
  235. curl_close($ch);
  236. return $_rs;
  237. }
  238. /**
  239. * 获取 jsapi_ticket
  240. * https://developers.weixin.qq.com/miniprogram/dev/api/token.html
  241. *
  242. * @param $access_token $appid 第三方用户唯一凭证
  243. *
  244. * @return string|bool
  245. */
  246. public static function getJsapiTicket1($access_token) {
  247. $_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
  248. $_param['type'] = 'jsapi';
  249. $_param['access_token'] = $access_token;
  250. $_rdata = self::curl($_url, http_build_query($_param));
  251. $_rdata = json_decode($_rdata, true);
  252. if (!isset($_rdata['ticket'])) {
  253. return false;
  254. }
  255. $_data['ticket'] = $_rdata['ticket'];
  256. $_data['expires_in'] = $_rdata['expires_in'];
  257. $_data['expires_time'] = time() + $_rdata['expires_in'];
  258. return $_data['ticket'];
  259. }
  260. }