AuthController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 思维定制
  5. * Date: 2019/3/6
  6. * Time: 10:17
  7. */
  8. namespace App\Http\Controllers\Web;
  9. use App\Models\AlbumAgentModel;
  10. use App\Models\AlbumManufacturerModel;
  11. use App\Models\AlbumUserModel;
  12. use Illuminate\Http\Request;
  13. use EasyWeChat\Factory;
  14. class AuthController extends Controller
  15. {
  16. public function bind(Request $request)
  17. {
  18. if ($request->isMethod('post')) {
  19. $store_id = $request->input('store_id');
  20. $phone = $request->input('phone');
  21. $name = $request->input('name');
  22. if (!$phone || !$name) {
  23. $message = '请输入电话号码';
  24. return view('web.auth.error', compact('message'));
  25. } else {
  26. $check = AlbumUserModel::where([['phone', $phone],['g_open_id', null],['is_dealer', 1]])->first();
  27. if (!$check) {
  28. $message = '该经销商号码不存在或是已绑定';
  29. return view('web.auth.error', compact('message'));
  30. }
  31. }
  32. $weChatApp = AlbumManufacturerModel::where('store_id', $store_id)->first();
  33. $config = [
  34. 'app_id' => $weChatApp->G_app_id,
  35. 'secret' => $weChatApp->G_app_secret,
  36. 'response_type' => 'array',
  37. 'oauth' => [
  38. 'scopes' => ['snsapi_userinfo'],
  39. 'callback' => '/web/notify?store_id=' . $store_id . "&name=$name&phone=$phone",
  40. ]
  41. ];
  42. $app = Factory::officialAccount($config);
  43. $response = $app->oauth
  44. ->redirect();
  45. return $response;
  46. } else {
  47. return view('web.auth.bind');
  48. }
  49. }
  50. public function notify(Request $request)
  51. {
  52. $store_id = $request->input('store_id');
  53. $phone = $request->input('phone');
  54. $name = $request->input('name');
  55. $weChatApp = AlbumManufacturerModel::where('store_id', $store_id)->first();
  56. $config = [
  57. 'app_id' => $weChatApp->G_app_id,
  58. 'secret' => $weChatApp->G_app_secret,
  59. 'response_type' => 'array',
  60. 'oauth' => [
  61. 'scopes' => ['snsapi_userinfo'],
  62. 'callback' => '/web/notify',
  63. ]
  64. ];
  65. $app = Factory::officialAccount($config);
  66. $oauth = $app->oauth;
  67. $user = $oauth->user()->toArray();
  68. $check_user = AlbumUserModel::where([['store_id',$store_id],['wechat_union_id',$user['original']['unionid']]])->first();
  69. $agent = AlbumAgentModel::where([['store_id', $store_id],['user_id', $check_user->id]])->first();
  70. $agent->name = $name;
  71. $agent->save();
  72. $check_user->g_open_id = $user['id'];
  73. $check_user->phone = $phone;
  74. $check_user->save();
  75. return view('web.auth.success');
  76. }
  77. public function notifyAccount(Request $request)
  78. {
  79. $store_id = $request->input('store_id');
  80. $config = [
  81. 'app_id' => 'wxbce144ca3da7aa23',
  82. 'secret' => '4d97eba675deeea187e1ebc32c1399da',
  83. 'response_type' => 'array',
  84. ];
  85. $app = Factory::officialAccount($config);
  86. if (isset($_GET["echostr"])) {
  87. $echoStr = $_GET["echostr"];
  88. //对接规则
  89. $signature = $_GET["signature"];
  90. $timestamp = $_GET["timestamp"];
  91. $nonce = $_GET["nonce"];
  92. $token = 'bshbdajdbjadwedwqer';
  93. $tmpArr = array($token, $timestamp, $nonce);
  94. sort($tmpArr, SORT_STRING);
  95. $tmpStr = implode($tmpArr);
  96. $tmpStr = sha1($tmpStr);
  97. if ($tmpStr == $signature) {
  98. echo $echoStr;
  99. } else {
  100. echo "";
  101. exit;
  102. }
  103. } else {
  104. $postStr = file_get_contents("php://input");
  105. if (!empty($postStr)) {
  106. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  107. $fromUsername = $postObj->FromUserName;
  108. $toUsername = $postObj->ToUserName;
  109. $MsgT = $postObj->MsgType;
  110. $time = time();
  111. //如果用户发的text类型
  112. if ($MsgT == "text") {
  113. $key = trim($postObj->Content);
  114. $fromUsername = $postObj->FromUserName;
  115. $textTpl = "<xml>
  116. <ToUserName><![CDATA[%s]]></ToUserName>
  117. <FromUserName><![CDATA[%s]]></FromUserName>
  118. <CreateTime>%s</CreateTime>
  119. <MsgType><![CDATA[%s]]></MsgType>
  120. <Content><![CDATA[%s]]></Content>
  121. </xml>";
  122. $msgType = "text";
  123. if ($key == '绑定') {
  124. $user = $app->user->get($fromUsername);
  125. $check_user = AlbumUserModel::where([['store_id',$store_id],['wechat_union_id',$user['unionid']]])->first();
  126. if ($check_user) {
  127. $check_user->g_open_id = $user['id'];
  128. $res = $check_user->save();
  129. } else {
  130. $data['username'] = $user['nickname'];
  131. $data['wechat_union_id'] = $user['unionid'];
  132. $data['avatar'] = $user['headimgurl'];
  133. $data['g_open_id'] = $fromUsername;
  134. $data['store_id'] = $store_id;
  135. $data['is_dealer'] = 0;
  136. $data['role'] = 0;
  137. $data['model'] = 0;
  138. $data['up_agent_id'] = 0;
  139. $res = AlbumUserModel::create($data);
  140. }
  141. if ($res) {
  142. $contentStr = "绑定成功";
  143. } else {
  144. $contentStr = "绑定失败";
  145. }
  146. } else {
  147. $contentStr = "回复 绑定 即可绑定用户";
  148. }
  149. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  150. echo $resultStr;
  151. exit;
  152. }
  153. //如果用户发的event(事件)类型
  154. if ($MsgT == "event") {
  155. $Event = $postObj->Event;
  156. if ($Event == 'subscribe') {
  157. $contentStr = "欢迎关注,回复 绑定 即可绑定用户";
  158. } else {
  159. $contentStr = "希望您下次关注,但您收不到此条消息了";
  160. }
  161. $textTpl = "<xml>
  162. <ToUserName><![CDATA[%s]]></ToUserName>
  163. <FromUserName><![CDATA[%s]]></FromUserName>
  164. <CreateTime>%s</CreateTime>
  165. <MsgType><![CDATA[%s]]></MsgType>
  166. <Content><![CDATA[%s]]></Content>
  167. </xml>";
  168. $Title = $postObj->Title;
  169. $Description = $postObj->Description;
  170. $Url = $postObj->Url;
  171. $msgType = 'text';
  172. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  173. echo $resultStr;
  174. exit;
  175. }
  176. } else {
  177. echo "";
  178. exit;
  179. }
  180. }
  181. }
  182. public function welcome()
  183. {
  184. return view('welcome');
  185. }
  186. }