Util.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. class Util
  4. {
  5. # 生成订单号
  6. public function createNumber($prefix, $table, $where = array())
  7. {
  8. $number = \Dever\Helper\Str::order($prefix);
  9. $where['order_num'] = $number;
  10. $state = Dever::db($table)->find($where);
  11. if (!$state) {
  12. return $number;
  13. } else {
  14. return $this->createNumber($prefix, $table, $where);
  15. }
  16. }
  17. # 获取openid 仅jspai和小程序需要openid
  18. public function openid($account, $env, $uid, $result = array())
  19. {
  20. $account = Dever::db('account', 'api')->find(array('key' => $account));
  21. if ($account) {
  22. $info = Dever::db('openid', 'api')->find(array('account_id' => $account['id'], 'uid' => $uid, 'env' => $env));
  23. if ($info) {
  24. $result['openid'] = $info['openid'];
  25. } else {
  26. if ($env == 3) {
  27. # jsapi 一般需要oauth授权
  28. if (empty($result['t'])) {
  29. $result['t'] = \Dever\Helper\Secure::login($uid);
  30. }
  31. $result['account'] = $account['key'];
  32. $result['link'] = Dever::url('api/oauth.code', $result);
  33. } elseif ($env == 5) {
  34. # 小程序
  35. $param['js_code'] = Dever::input('applet_code', 'is_string', '登录信息');
  36. $data = Dever::load('account', 'api')->run($account, 'applet_login', $param, $env);
  37. if (isset($data['openid'])) {
  38. $result['openid'] = $data['openid'];
  39. $update['uid'] = $uid;
  40. $update['account_id'] = $account['id'];
  41. $update['env'] = $env;
  42. $update['openid'] = $data['openid'];
  43. Dever::db('openid', 'api')->insert($update);
  44. }
  45. }
  46. }
  47. }
  48. return $result;
  49. }
  50. # 获取参数类型
  51. public function fieldType($platform_id)
  52. {
  53. $data = array();
  54. $data[] = array
  55. (
  56. 'id' => 1,
  57. 'name' => '格式转换',
  58. 'children' => Dever::db('format', 'api')->select([]),
  59. );
  60. $where = ['platform_id' => $platform_id];
  61. $data[] = array
  62. (
  63. 'id' => 2,
  64. 'name' => '加密',
  65. 'children' => Dever::db('platform_ssl', 'api')->select($where),
  66. );
  67. $data[] = array
  68. (
  69. 'id' => 3,
  70. 'name' => '签名',
  71. 'children' => Dever::db('platform_sign', 'api')->select($where),
  72. );
  73. return $data;
  74. }
  75. # 获取签名列表
  76. public function getPlatformSign($platform_id)
  77. {
  78. return Dever::db('platform_sign', 'api')->select(array('platform_id' => $platform_id));
  79. }
  80. # 格式转换
  81. public function format($id, $value)
  82. {
  83. $info = Dever::db('format', 'api')->find($id);
  84. if ($info) {
  85. $info['method'] = str_replace('{value}', "'{value}'", $info['method']);
  86. $value = \Dever\Helper\Str::val($info['method'], array('value' => $value));
  87. }
  88. return $value;
  89. }
  90. }