💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 第六课微擎高级操作 - token获取 ``` <pre class="calibre14">``` $token <span class="token">=</span> WeAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">token</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - - 加载类 ``` <pre class="calibre14">``` $weixin <span class="token">=</span> weixinpay<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span><span class="token4">'wechat'</span><span class="token2">)</span><span class="token2">;</span> $userinfo <span class="token">=</span> $weixin<span class="token">-</span><span class="token">></span><span class="token1">getOauthInfo</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` ``` <pre class="calibre14">``` <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">model</span><span class="token2">(</span><span class="token4">'mc'</span><span class="token2">)</span><span class="token2">;</span> $wechat <span class="token">=</span> $_W<span class="token2">[</span><span class="token4">'account'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'setting'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'payment'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'wechat'</span><span class="token2">]</span><span class="token2">;</span><span class="token6">//支付设置</span> $wechat <span class="token">=</span><span class="token2">(</span><span class="token">!</span><span class="token1">empty</span><span class="token2">(</span>$wechat<span class="token2">[</span><span class="token4">'borrow'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span><span class="token">?</span> <span class="token1">uni_setting</span><span class="token2">(</span>$wechat<span class="token2">[</span><span class="token4">'borrow'</span><span class="token2">]</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'payment'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">[</span><span class="token4">'payment'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'wechat'</span><span class="token2">]</span><span class="token2">:</span>$wechat<span class="token2">;</span> ``` ``` 长网址转换短网址 ``` <pre class="calibre14">``` <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">classs</span><span class="token2">(</span><span class="token4">'weixin.account'</span><span class="token2">)</span><span class="token2">;</span> $acc <span class="token">=</span> WeiXinAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>$_W<span class="token2">[</span><span class="token4">'acid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $url <span class="token">=</span> $acc<span class="token">-</span><span class="token">></span><span class="token1">long2short</span><span class="token2">(</span><span class="token4">"http://www.baidu.com"</span><span class="token2">)</span><span class="token2">[</span><span class="token4">'short_url'</span><span class="token2">]</span><span class="token2">;</span> ``` ``` 文件上传,返回media\_id ``` <pre class="calibre14">``` <span class="token5">if</span><span class="token2">(</span><span class="token1">is_file</span><span class="token2">(</span>ATTACHMENT_ROOT<span class="token2">.</span><span class="token4">"/1.jpg"</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> $path <span class="token">=</span><span class="token4">"1.jpg"</span><span class="token2">;</span> <span class="token2">}</span><span class="token5">else</span><span class="token2">{</span> <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">func</span><span class="token2">(</span><span class="token4">'file'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">file_move</span><span class="token2">(</span>MODULE_ROOT<span class="token2">.</span><span class="token4">"/1.jpg"</span><span class="token2">,</span>ATTACHMENT_ROOT<span class="token2">.</span><span class="token4">"/1.jpg"</span><span class="token2">)</span><span class="token2">;</span> $path <span class="token">=</span><span class="token4">"1.jpg"</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">classs</span><span class="token2">(</span><span class="token4">'weixin.account'</span><span class="token2">)</span><span class="token2">;</span> $acc <span class="token">=</span> WeiXinAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>$_W<span class="token2">[</span><span class="token4">'acid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $media <span class="token">=</span> $acc<span class="token">-</span><span class="token">></span><span class="token1">uploadMedia</span><span class="token2">(</span>$path<span class="token2">)</span><span class="token2">;</span> <span class="token1">dump</span><span class="token2">(</span>$media<span class="token2">)</span><span class="token2">;</span> Array <span class="token2">(</span> <span class="token2">[</span>type<span class="token2">]</span> <span class="token">=</span><span class="token">></span> image <span class="token2">[</span>media_id<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">4</span>IftJVo5o<span class="token">-</span>qS2hm0Q0ID<span class="token">-</span>_m5VARGL2eqKsgT1ih7v63SA8pULoqplVzIEU4hdt7y <span class="token2">[</span>created_at<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">1515481289</span> <span class="token2">)</span> ``` ``` 下载文件 ``` <pre class="calibre14">``` <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">classs</span><span class="token2">(</span><span class="token4">'weixin.account'</span><span class="token2">)</span><span class="token2">;</span> $acc <span class="token">=</span> WeiXinAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>$_W<span class="token2">[</span><span class="token4">'acid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $arr <span class="token">=</span> <span class="token2">[</span><span class="token4">'media_id'</span><span class="token">=</span><span class="token">></span><span class="token4">"4IftJVo5o-qS2hm0Q0ID-_m5VARGL2eqKsgT1ih7v63SA8pULoqplVzIEU4hdt7y"</span><span class="token2">,</span><span class="token4">'type'</span><span class="token">=</span><span class="token">></span><span class="token4">"image"</span><span class="token2">]</span><span class="token2">;</span> $media <span class="token">=</span> $acc<span class="token">-</span><span class="token">></span><span class="token1">downloadMedia</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> <span class="token6">//images/5/2018/01/4IftJVo5o-qS2hm0Q0ID-_m5VARGL2eqKsgT1ih7v63SA8pULoqplVzIEU4hdt7y.jpg 返回路径</span> <span class="token">~</span><span class="token">~</span><span class="token">~</span> 读取与写入实实配置 ``` ``` $config = pdo\_getcolumn('uni\_account\_modules',\[ 'module' => 模块名, 'uniacid' => $\_W\['uniacid'\] \],'settings'); $config = iunserializer($config); pdo\_update('uni\_account\_modules',\[ 'settings' =>iserializer($config), \], \[ 'module' =>模块名, 'uniacid' => $\_W\['uniacid'\] \]); ``` <pre class="calibre16">``` 发送客服消息 <span class="token">~</span><span class="token">~</span><span class="token">~</span> $info <span class="token">=</span> <span class="token4">"我是客服消息:\n 欢迎使用"</span><span class="token2">;</span> $openid <span class="token">=</span> <span class="token4">"ocUeXwINHvRqWPSldYZVO4dA_CzI"</span><span class="token2">;</span> $message <span class="token">=</span> <span class="token2">[</span> <span class="token4">'msgtype'</span><span class="token">=</span><span class="token">></span><span class="token4">'text'</span><span class="token2">,</span> <span class="token4">'text'</span><span class="token">=</span><span class="token">></span><span class="token2">[</span><span class="token4">'content'</span><span class="token">=</span><span class="token">></span><span class="token1">urlencode</span><span class="token2">(</span>$info<span class="token2">)</span><span class="token2">]</span><span class="token2">,</span> <span class="token4">'touser'</span><span class="token">=</span><span class="token">></span>$openid <span class="token2">]</span><span class="token2">;</span> $acc <span class="token">=</span> WeAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $status <span class="token">=</span> $acc<span class="token">-</span><span class="token">></span><span class="token1">sendCustomNotice</span><span class="token2">(</span>$message<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">is_error</span><span class="token2">(</span>$status<span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token1">message</span><span class="token2">(</span><span class="token4">'发送失败,原因为'</span> <span class="token2">.</span> $status<span class="token2">[</span><span class="token4">'message'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $message <span class="token">=</span> <span class="token2">[</span> <span class="token4">'touser'</span> <span class="token">=</span><span class="token">></span> $openid<span class="token2">,</span> <span class="token4">'msgtype'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'image'</span><span class="token2">,</span> <span class="token4">'image'</span> <span class="token">=</span><span class="token">></span><span class="token2">[</span><span class="token4">'media_id'</span><span class="token">=</span><span class="token">></span> <span class="token4">'MEDIA_ID'</span><span class="token2">]</span><span class="token6">//微信素材media_id,微擎中微信上传组件可以得到此值</span> <span class="token2">]</span><span class="token2">;</span> $message <span class="token">=</span> <span class="token2">[</span> <span class="token4">'touser'</span> <span class="token">=</span><span class="token">></span> $openid<span class="token2">,</span> <span class="token4">'msgtype'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'music'</span><span class="token2">,</span> <span class="token4">'music'</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span> <span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'标题'</span><span class="token2">,</span> <span class="token4">'description'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'描述'</span> <span class="token4">'musicurl'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'音乐文件URL'</span><span class="token2">,</span> <span class="token4">'hqmusicurl'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'高清音乐文件URL'</span><span class="token2">,</span> <span class="token4">'thumb_media_id'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'MEDIA_ID'</span><span class="token2">,</span> <span class="token2">]</span> <span class="token2">]</span><span class="token2">;</span> $message <span class="token">=</span> <span class="token2">[</span> <span class="token4">'touser'</span> <span class="token">=</span><span class="token">></span> $openid<span class="token2">,</span> <span class="token4">'msgtype'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'news'</span><span class="token2">,</span> <span class="token4">'news'</span> <span class="token">=</span><span class="token">></span><span class="token2">[</span> <span class="token2">[</span> <span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'标题'</span><span class="token2">,</span> <span class="token4">'description'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'描述'</span><span class="token2">,</span> <span class="token4">'url'</span> <span class="token2">:</span> <span class="token4">'链接'</span><span class="token2">,</span> <span class="token4">'picurl'</span> <span class="token2">:</span> <span class="token4">'封面图片地址'</span> <span class="token2">]</span><span class="token2">,</span> <span class="token2">[</span> <span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'标题1'</span><span class="token2">,</span> <span class="token4">'description'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'描述1'</span><span class="token2">,</span> <span class="token4">'url'</span> <span class="token2">:</span> <span class="token4">'链接'</span><span class="token2">,</span> <span class="token4">'picurl'</span> <span class="token2">:</span> <span class="token4">'封面图片地址'</span> <span class="token2">]</span><span class="token2">,</span> <span class="token2">]</span> <span class="token2">]</span><span class="token2">;</span> $message <span class="token">=</span> <span class="token2">[</span> <span class="token4">'touser'</span> <span class="token">=</span><span class="token">></span> $openid<span class="token2">,</span> <span class="token4">'msgtype'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'wxcard'</span><span class="token2">,</span> <span class="token4">'wxcard'</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span><span class="token4">'card_id'</span><span class="token">=</span><span class="token">></span> <span class="token4">'CARD_ID'</span><span class="token2">]</span> <span class="token2">]</span><span class="token2">;</span> <span class="token">~</span><span class="token">~</span><span class="token">~</span> 发送模板消息 <span class="token">~</span><span class="token">~</span><span class="token">~</span> $tpl_id <span class="token">=</span> <span class="token4">"QDDIbJpHEann2VeN12r6GXf0OiTJMky8uL4FAK2zAp8"</span><span class="token2">;</span> $openid <span class="token">=</span> <span class="token4">"ocUeXwINHvRqWPSldYZVO4dA_CzI"</span><span class="token2">;</span> $url <span class="token">=</span> <span class="token4">""</span><span class="token2">;</span> $arr <span class="token">=</span><span class="token2">[</span><span class="token2">]</span><span class="token2">;</span> $sendarr <span class="token">=</span><span class="token2">[</span><span class="token4">'测试通知标题'</span><span class="token2">,</span><span class="token4">'西安大学'</span><span class="token2">,</span><span class="token4">'管理员'</span><span class="token2">,</span><span class="token4">'2018-4-28'</span><span class="token2">,</span><span class="token4">'商城决定展示关闭'</span><span class="token2">,</span><span class="token4">'点击查看详情'</span><span class="token2">]</span><span class="token2">;</span> $tpl <span class="token">=</span> '<span class="token2">{</span><span class="token2">{</span>first<span class="token2">.</span>DATA<span class="token2">}</span><span class="token2">}</span> 学校:<span class="token2">{</span><span class="token2">{</span>keyword1<span class="token2">.</span>DATA<span class="token2">}</span><span class="token2">}</span> 通知人:<span class="token2">{</span><span class="token2">{</span>keyword2<span class="token2">.</span>DATA<span class="token2">}</span><span class="token2">}</span> 时间:<span class="token2">{</span><span class="token2">{</span>keyword3<span class="token2">.</span>DATA<span class="token2">}</span><span class="token2">}</span> 通知内容:<span class="token2">{</span><span class="token2">{</span>keyword4<span class="token2">.</span>DATA<span class="token2">}</span><span class="token2">}</span>'<span class="token2">;</span> <span class="token1">preg_match_all</span><span class="token2">(</span><span class="token4">'/{{(.*).DATA}}/'</span><span class="token2">,</span>$tpl<span class="token2">,</span>$rs<span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$rs<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span> as $k<span class="token">=</span><span class="token">></span>$v<span class="token2">)</span><span class="token2">{</span> $arr<span class="token2">[</span>$v<span class="token2">]</span> <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token4">'value'</span><span class="token">=</span><span class="token">></span>$sendarr<span class="token2">[</span>$k<span class="token2">]</span> <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $arr<span class="token2">[</span><span class="token4">'first'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'color'</span><span class="token2">]</span><span class="token">=</span><span class="token4">'#04be02'</span><span class="token2">;</span> $arr<span class="token2">[</span><span class="token4">'remark'</span><span class="token2">]</span><span class="token2">[</span><span class="token4">'color'</span><span class="token2">]</span><span class="token">=</span><span class="token4">'#18b4ed'</span><span class="token2">;</span> $acc <span class="token">=</span> WeAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $status <span class="token">=</span> $acc<span class="token">-</span><span class="token">></span><span class="token1">sendTplNotice</span><span class="token2">(</span>$openid<span class="token2">,</span>$tpl_id<span class="token2">,</span> $arr<span class="token2">,</span> $url<span class="token2">)</span><span class="token2">;</span> <span class="token6">//1表示发送成功 </span> <span class="token">~</span><span class="token">~</span><span class="token">~</span> 获取用户信息oauth <span class="token">~</span><span class="token">~</span><span class="token">~</span> <span class="token1">load</span><span class="token2">(</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">classs</span><span class="token2">(</span><span class="token4">'weixin.account'</span><span class="token2">)</span><span class="token2">;</span> $acc <span class="token">=</span> WeiXinAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>$_W<span class="token2">[</span><span class="token4">'acid'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $userinfo <span class="token">=</span>$acc<span class="token">-</span><span class="token">></span><span class="token1">fansQueryInfo</span><span class="token2">(</span>$openid<span class="token2">)</span><span class="token2">;</span> <span class="token">~</span><span class="token">~</span><span class="token">~</span> ``` ```